Authored: Kathy N. Lam
Date: January 13, 2020
Updated: April 12, 2021
library(flowCore) #for reading and manipulating flow data
library(ggcyto) #for using ggplot with flow data
library(scales) #for nice plot axes
library(cowplot) #for multi panel plots
library(Phenoflow) #for rarefying
library(tidyverse) #for data wrangling and plotting
(fs = read.flowSet(path="data_fcs"))
## A flowSet with 100 experiments.
##
## column names:
## 530/30 Blue B-A 610/20 YG C-A FSC-A FSC-H FSC-W SSC-A SSC-H SSC-W Time
colnames(fs)
## [1] "530/30 Blue B-A" "610/20 YG C-A" "FSC-A" "FSC-H"
## [5] "FSC-W" "SSC-A" "SSC-H" "SSC-W"
## [9] "Time"
pData(phenoData(fs))
#read in sample info
metadata = read_tsv("metadata.tsv") %>%
rename(name=Filename) %>%
mutate(Mouse = paste("Mouse", Mouse)) %>%
mutate(Day = paste("Day", Timepoint)) %>%
mutate(Treatment_Mouse = paste0(Treatment, "\n", Mouse)) %>%
mutate(Mouse_Treatment = paste0(Mouse, " (", Treatment, ")"))
## Parsed with column specification:
## cols(
## FlowSampleNumber = col_double(),
## `Mouse Sample Number` = col_double(),
## Experiment = col_character(),
## Treatment = col_character(),
## Mouse = col_double(),
## Timepoint = col_double(),
## Processing = col_character(),
## CollectEvents = col_character(),
## Filename = col_character(),
## Notes = col_logical()
## )
metadata$Mouse = factor(metadata$Mouse, levels=unique(metadata$Mouse))
metadata$Treatment_Mouse = factor(metadata$Treatment_Mouse, levels=unique(metadata$Treatment_Mouse))
metadata$Treatment = factor(metadata$Treatment, levels=unique(metadata $Treatment))
metadata$Day = factor(metadata$Day, levels=unique(metadata$Day))
metadata
#add columns to phenoData
phenoData(fs)$Order = seq(1, length(phenoData(fs)$name))
phenoData(fs)$Treatment = metadata$Treatment
phenoData(fs)$Timepoint = metadata$Timepoint
phenoData(fs)$Day = metadata$Day
phenoData(fs)$Name = metadata$name
phenoData(fs)$Mouse = metadata$Mouse
phenoData(fs)$Treatment_Mouse = metadata$Treatment_Mouse
phenoData(fs)$Mouse_Treatment = metadata$Mouse_Treatment
pData(phenoData(fs))
#make labeller function for facet_wrap
order = as.character(phenoData(fs)$Order)
name = phenoData(fs)$Name
order_names = mapply(c, order, name, SIMPLIFY = FALSE) #make a one-to-one
order_names = lapply(order_names, `[[`, 2) #keep second element of each vector in the list
order_names = order_names[as.character(sort(as.numeric(names(order_names))))] #numerically sort
order_labeller = function(variable,value){
return(order_names[value])
}
scatter = rbind(c(0, 1e4),
c(1e5, 1e4),
c(1e5, 2.5e5),
c(0, 2.5e5))
colnames(scatter)=c("FSC-A", "SSC-A")
scatter = as.data.frame(scatter)
ggplot() +
geom_point(data=fs, aes(x=`FSC-A`, y=`SSC-A`), shape=16, size=0.75, alpha=0.5) +
scale_y_continuous(name="SSC-A (Granularity)\n", limits = c(-2e1,3e5)) +
scale_x_continuous(name="\nFSC-A (Size)", limit=c(-2e1,3e5)) +
facet_grid(Mouse~Timepoint) +
theme_linedraw(14) +
theme(panel.grid = element_blank(), axis.text.x=element_text(angle=90,hjust=1)) +
geom_polygon(data=scatter, aes(x=`FSC-A`, y=`SSC-A`), fill=NA, colour="indianred", size=0.5, linetype="solid")
## Warning: Removed 29191 rows containing missing values (geom_point).
gate_scatter = polygonGate(filterId="scatter", `FSC-A` = scatter$`FSC-A`, `SSC-A` = scatter$`SSC-A`)
result = flowCore::filter(fs, gate_scatter)
events = flowCore::Subset(fs, result)
events = Phenoflow::FCS_resample(events, replace = FALSE)
## Your samples range between 57249 and 90877 cells
## Your samples were randomly subsampled to 57249 cells
sessionInfo()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] forcats_0.5.0 stringr_1.4.0
## [3] dplyr_1.0.4 purrr_0.3.4
## [5] readr_1.3.1 tidyr_1.1.0
## [7] tibble_3.0.6 tidyverse_1.3.0
## [9] Phenoflow_1.1.2 foreach_1.5.0
## [11] flowAI_1.19.1 flowFDA_0.99
## [13] mclust_5.4.6 multcomp_1.4-13
## [15] TH.data_1.0-10 MASS_7.3-53
## [17] survival_3.2-7 mvtnorm_1.1-0
## [19] flowFP_1.47.0 flowViz_1.53.0
## [21] lattice_0.20-41 flowClean_1.27.0
## [23] cowplot_1.0.0 scales_1.1.1
## [25] ggcyto_1.17.0 flowWorkspace_4.1.1
## [27] ncdfFlow_2.35.0 BH_1.72.0-3
## [29] RcppArmadillo_0.9.900.1.0 ggplot2_3.3.2
## [31] flowCore_2.1.0
##
## loaded via a namespace (and not attached):
## [1] readxl_1.3.1 changepoint_2.2.2 backports_1.1.8
## [4] plyr_1.8.6 igraph_1.2.5 splines_4.0.3
## [7] digest_0.6.25 htmltools_0.4.0 fansi_0.4.1
## [10] magrittr_1.5 cluster_2.1.0 sfsmisc_1.1-7
## [13] recipes_0.1.12 Biostrings_2.57.1 modelr_0.1.8
## [16] gower_0.2.1 RcppParallel_5.0.1 matrixStats_0.56.0
## [19] sandwich_2.5-1 cytolib_2.1.2 jpeg_0.1-8.1
## [22] colorspace_1.4-1 rvest_0.3.5 blob_1.2.1
## [25] haven_2.3.1 xfun_0.14 crayon_1.3.4
## [28] jsonlite_1.6.1 hexbin_1.28.1 graph_1.67.1
## [31] zoo_1.8-8 iterators_1.0.12 ape_5.4
## [34] glue_1.4.2 gtable_0.3.0 ipred_0.9-9
## [37] zlibbioc_1.35.0 XVector_0.29.1 phyloseq_1.33.0
## [40] IDPmisc_1.1.20 Rgraphviz_2.33.0 Rhdf5lib_1.11.0
## [43] BiocGenerics_0.35.3 DBI_1.1.0 Rcpp_1.0.4.6
## [46] bit_1.1-15.2 stats4_4.0.3 lava_1.6.7
## [49] prodlim_2019.11.13 httr_1.4.1 RColorBrewer_1.1-2
## [52] ellipsis_0.3.1 farver_2.0.3 pkgconfig_2.0.3
## [55] XML_3.99-0.3 nnet_7.3-14 dbplyr_1.4.4
## [58] caret_6.0-86 labeling_0.3 tidyselect_1.1.0
## [61] rlang_0.4.10 reshape2_1.4.4 munsell_0.5.0
## [64] cellranger_1.1.0 tools_4.0.3 cli_2.0.2
## [67] generics_0.1.0 ade4_1.7-15 broom_0.5.6
## [70] evaluate_0.14 biomformat_1.17.0 yaml_2.2.1
## [73] ModelMetrics_1.2.2.2 knitr_1.28 fs_1.4.1
## [76] nlme_3.1-149 xml2_1.3.2 rstudioapi_0.11
## [79] compiler_4.0.3 png_0.1-7 reprex_0.3.0
## [82] stringi_1.4.6 Matrix_1.2-18 vegan_2.5-6
## [85] permute_0.9-5 multtest_2.45.0 vctrs_0.3.6
## [88] pillar_1.4.4 lifecycle_0.2.0 data.table_1.12.8
## [91] R6_2.4.1 latticeExtra_0.6-29 KernSmooth_2.23-17
## [94] gridExtra_2.3 RProtoBufLib_2.1.0 IRanges_2.23.6
## [97] codetools_0.2-16 boot_1.3-25 assertthat_0.2.1
## [100] rhdf5_2.33.0 withr_2.2.0 S4Vectors_0.27.10
## [103] mgcv_1.8-33 parallel_4.0.3 hms_0.5.3
## [106] grid_4.0.3 rpart_4.1-15 timeDate_3043.102
## [109] class_7.3-17 rmarkdown_2.2 pROC_1.16.2
## [112] Biobase_2.49.0 lubridate_1.7.9.2